//
//  FavoriteViewController.m
//  ios_news
//
//  Created by Han on 14-7-22.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "FavoriteViewController.h"

@implementation FavoriteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}




- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = MAIN_COLOR;
    self.navigationController.navigationBar.translucent = NO;  // 半透明
    
    self.navigationItem.title = @"收藏";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    [self addButton];
    
    
    float mTableViewHeight = GLOBLE_HEIGHT - 64; // nav 64
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, mTableViewHeight)];
    
    mTableView.delegate = self;
    mTableView.dataSource = self;
    
    
    mData = [NSMutableArray arrayWithCapacity:5];
    
    LibSqlite *db = [LibSqlite shareInstance];
    mData = [db getAll:@"SELECT * FROM favorite_news ORDER BY fav_id DESC"];
    
    
    [self.view addSubview:mTableView];
}


//添加按钮
-(void)addButton
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.exclusiveTouch = YES;
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    //button.backgroundColor = [UIColor redColor];
    
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    
    [leftButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    //编辑 搜索按钮
    mEditButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    mEditButton.exclusiveTouch = YES;
    [mEditButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [mEditButton setTitle:@"编辑" forState:UIControlStateNormal];
    [mEditButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [mEditButton setBackgroundColor:[UIColor clearColor]];
    [mEditButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:mEditButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void) buttonClicked:(id)sender
{
    mEditButton.selected = !mEditButton.isSelected;
    if (sender == mEditButton) {
        if (mEditButton.isSelected) {
            _isEdit = YES;
            [mEditButton setTitle:@"完成" forState:UIControlStateNormal];
            [mTableView setEditing:YES animated:YES];
        } else {
            _isEdit = NO;
            [mTableView setEditing:NO animated:YES];
            [mEditButton setTitle:@"编辑" forState:UIControlStateNormal];
        }
    }
}


-(void)backButtonClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"FavCell";
    FavCell *cell = (FavCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[FavCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
    }
    
    //cell.liveNews = [mData objectAtIndex:(NSUInteger)indexPath.row];
    cell.titleLb.text = [[mData objectAtIndex:(NSUInteger)indexPath.row] objectAtIndex:3];
    cell.timeLb.text = [[mData objectAtIndex:(NSUInteger)indexPath.row] objectAtIndex:6];
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return mData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}


-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除";
}

/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    return _isEdit ? YES : NO;
    return YES;
    //return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        LibSqlite *db = [LibSqlite shareInstance];
        [db execute:[NSString stringWithFormat:@"DELETE FROM favorite_news WHERE fav_id='%@'", [[mData objectAtIndex:(NSUInteger)indexPath.row] objectAtIndex:0]]];
        
        
        [mData removeObjectAtIndex:indexPath.row];
        [mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]  withRowAnimation:UITableViewRowAnimationFade];
    }
    /*
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }
     */
}



@end
